package ${packageOutPath}.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.mockito.internal.debugging.VerboseMockInvocationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cykj.common.api.users.vo.Users;
import com.cykj.common.cymall_utils.Constants;
import com.cykj.common.cymall_utils.UserUtils;
import com.cykj.common.utils.PageHelper;


import ${packageOutPath}.URL;
import ${packageOutPath}.bean.${bean};
import ${packageOutPath}.service.I${bean}Service;

import ${packageOutPath}.vo.${bean}Vo;

@Controller
public class ${bean}Controller {
	
	@Autowired
	private I${bean}Service service;
	
	/**
	 * 跳转到列表页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value=URL.req_${lowercaseBean}_list)
	public ModelAndView queryAll(Integer pageNumber, HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		//分页的当前页
		if(null==pageNumber){
			pageNumber=1;
		}
		
		//设置跳转页面
		mav.setViewName(URL.jsp_${lowercaseBean}_list);
		Users user = UserUtils.getUser(request);
		PageRequest pageRequest=new PageRequest(pageNumber-1, 10);//当前页和每页显示的条数
		
		//因为是逻辑删除，所有要设置条件
		${bean}Vo ${lowercaseBean}Vo = new ${bean}Vo();
		${lowercaseBean}Vo.setOid(user.getOid());
		${lowercaseBean}Vo.setIsShow(1);
		
		
		PageHelper<${bean}Vo> page=service.queryPage(pageRequest, ${lowercaseBean}Vo);
		
		mav.addObject("page", page);
		
		
		return mav;
	}
	
	/**
	 * 跳转到新增页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value=URL.req_${lowercaseBean}_toInput)
	public ModelAndView toAdd(HttpServletRequest request,Integer id){
		ModelAndView mav = new ModelAndView();
		if(null != id){
			${bean}Vo ${lowercaseBean}Vo = service.getById(id);
			mav.getModel().put("bean", ${lowercaseBean}Vo);
		}
		mav.setViewName(URL.jsp_${lowercaseBean}_input);	
		return mav;
	}
	
	/**
	 * 跳转查看
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value=URL.req_${lowercaseBean}_toView)
	public ModelAndView toView(Integer id, HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		if(null != id){
			${bean}Vo ${lowercaseBean}Vo = service.getById(id);
			mav.getModel().put("bean", ${lowercaseBean}Vo);
		}
		
		mav.setViewName(URL.jsp_${lowercaseBean}_view);	
		
		return mav;
	}
	
	/**
	 * 保存
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value=URL.req_${lowercaseBean}_addOrUpdate)
	@ResponseBody
	public Map<String, Object> addOrUpdate(${bean}Vo ${lowercaseBean}Vo, HttpServletRequest request){
		Map<String,Object> res = new HashMap<String, Object>();
		String code = Constants.SUCCESS ;
		String msg =  Constants.SUCCESS_TXT ;
		// 获取用户对象
		Users user = UserUtils.getUser(request) ;
		${lowercaseBean}Vo.setOid(user.getOid());
		
		// 修改
		if (${lowercaseBean}Vo.getSeqId() != null) {
			res = service.update(${lowercaseBean}Vo, user);
		} else {
			// 保存
			res = service.save(${lowercaseBean}Vo, user);
		}
			
		return res;
	}
	
	
	/**
	 * 删除
	 * @param id
	 * @param ids
	 * @return
	 */
	@RequestMapping(value=URL.req_${lowercaseBean}_delete)
	@ResponseBody
	public Map<String, Object> delete(Integer id, String ids){
		Map<String,Object> res = new HashMap<String, Object>();
		String code = Constants.SUCCESS ;
		String msg =  Constants.SUCCESS_TXT ;
		
		if(null != ids){
			String[] array = ids.split("\\,");
			for (String _id : array) {
				res = service.deleteById(Integer.parseInt(_id));
			}
		}else{
			res = service.deleteById(id);
		}
		
		return res;
	}
	
	
	
}
